package com.example.myinput;

import android.os.Build;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class MySocket {
    public static void run(){
        try {
            ServerSocket serverSocket = new ServerSocket(8765, 0, InetAddress.getByName("127.0.0.1"));
            System.out.println("Server started on port 8765");
            while (true) {
                Socket client = serverSocket.accept();
                BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
                OutputStream out = client.getOutputStream();
                String line = reader.readLine();
                System.out.println("Received: " + line);
                try {
                    JSONObject data=new JSONObject(line);
                    if(data.has("action")){
                        switch (data.getString("action")){
                            case MyInput.TOUCH:
                                if (Touch.touchCheck(data.getString("eventName"),(float)(data.getDouble("x")),(float)data.getDouble("y"))){
                                    Touch.touch(data.getString("eventName"),(float)data.getDouble("x"),(float)data.getDouble("y"));
                                }
                                break;
                            case MyInput.SWIPE:
                                if (Touch.checkSwipe((float)data.getDouble("x1"),(float)data.getDouble("y1"),(float)data.getDouble("x2"),(float)data.getDouble("y2"),data.getLong("durationMs"))){
                                    Touch.swipe((float)data.getDouble("x1"),(float)data.getDouble("y1"),(float)data.getDouble("x2"),(float)data.getDouble("y2"),data.getLong("durationMs"));
                                }
                                break;
                            case MyInput.SWIPEBEZIER:
                                if(Touch.checkSwipe((float)data.getDouble("x1"),(float)data.getDouble("y1"),(float)data.getDouble("x2"),(float)data.getDouble("y2"),data.getLong("durationMs"))){
                                    Touch.swipeBezier((float)data.getDouble("x1"),(float)data.getDouble("y1"),(float)data.getDouble("x2"),(float)data.getDouble("y2"),data.getLong("durationMs"));
                                }
                                break;
                            case MyInput.FENDUANBEZIER:
                                if(Touch.checkFenDuan((float)data.getDouble("x1"),(float)data.getDouble("y1"),(float)data.getDouble("x2"),(float)data.getDouble("y2"),data.getInt("nums"),(float)data.getDouble("mLeaveHeightLength"),(float)data.getDouble("mh"),data.getLong("durationMs"),data.getInt("willContinue"))){
                                    Touch.fenDuan((float)data.getDouble("x1"),(float)data.getDouble("y1"),(float)data.getDouble("x2"),(float)data.getDouble("y2"),data.getInt("nums"),(float)data.getDouble("mLeaveHeightLength"),(float)data.getDouble("mh"),data.getLong("durationMs"),data.getInt("willContinue"),data.getInt("pressure"));
                                }
                                break;
                            case MyInput.FENDUANTRAJECTORY:
                                if(Touch.checkFenDuanTrajectory((float)data.getDouble("x1"),(float)data.getDouble("y1"),(float)data.getDouble("x2"),(float)data.getDouble("y2"),data.getInt("nums"),data.getLong("durationMs"),data.getInt("willContinue"))){
                                    Touch.fenDuanTrajectory((float)data.getDouble("x1"),(float)data.getDouble("y1"),(float)data.getDouble("x2"),(float)data.getDouble("y2"),data.getInt("nums"),data.getLong("durationMs"),data.getInt("willContinue"),data.getInt("pressure"));
                                }
                                break;
                            case MyInput.TOUCHSTOP:
                                if (Touch.touchCheck(data.getInt("x"),data.getInt("y"),data.getInt("num"),data.getInt("durationMs"))){
                                    if(Build.VERSION.SDK_INT<29){
                                        Touch.touchStop(data.getInt("x"),data.getInt("y"),data.getInt("num"),data.getInt("durationMs"),data.getInt("pressure"),data.getInt("isUp"));
                                    }
                                }
                                break;
                            case MyInput.EVENTBEZIER:
                                if(Touch.checkFenDuan(data.getInt("x1"),data.getInt("y1"),data.getInt("x2"),data.getInt("y2"),data.getInt("nums"),data.getInt("mLeaveHeightLength"),data.getInt("mh"),data.getInt("durationMs"),data.getInt("willContinue"))){
                                    if(Build.VERSION.SDK_INT<29) {
                                        Touch.injectBezier(data.getInt("x1"), data.getInt("y1"), data.getInt("x2"), data.getInt("y2"), data.getInt("nums"), data.getInt("mLeaveHeightLength"), data.getInt("mh"), data.getInt("durationMs"), data.getInt("willContinue"), data.getInt("pressure"));
                                    }else{
                                        Touch.fenDuan((float)data.getInt("x1"),(float)data.getInt("y1"),(float)data.getInt("x2"),(float)data.getInt("y2"),data.getInt("nums"),(float)data.getInt("mLeaveHeightLength"),(float)data.getInt("mh"),data.getLong("durationMs"),data.getInt("willContinue"),data.getInt("pressure"));
                                    }
                                }
                                break;
                            case MyInput.EVENTTRAJECTORY:
                                if(Touch.checkFenDuanTrajectory(data.getInt("x1"),data.getInt("y1"),data.getInt("x2"),data.getInt("y2"),data.getInt("nums"),data.getInt("durationMs"),data.getInt("willContinue"))){
                                    if(Build.VERSION.SDK_INT<29) {
                                        Touch.injectTrajectory(data.getInt("x1"), data.getInt("y1"), data.getInt("x2"), data.getInt("y2"), data.getInt("nums"), data.getInt("durationMs"), data.getInt("willContinue"), data.getInt("pressure"));
                                    }else{
                                        Touch.fenDuanTrajectory((float)data.getInt("x1"),(float)data.getInt("y1"),(float)data.getInt("x2"),(float)data.getInt("y2"),data.getInt("nums"),data.getLong("durationMs"),data.getInt("willContinue"),data.getInt("pressure"));
                                    }
                                }
                                break;
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                out.write("OK\n".getBytes());
                out.flush();
                client.close();
            }
        } catch (Exception e) {
            // ignore
            e.printStackTrace();
        }
    }
}
